# -*- coding: utf-8 -*-
# @Date   : 2020/3/3
# @File   : R11_EG_CorporationShareRatioCheck_OTO.py


'''eof
name:企业法定代表人股比比例校验
code:R11_EG_CorporationShareRatioCheck_OTO
tableName:
columnName:
groups:企业业务信息校验
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import json


def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


def float_check(target):
    try:
        float(target)
        return True
    except:
        return False


def getRule11():
    null_type_list = ['', ' ',  None, 'null', 'Null', 'NULL', '/', '-', '--']
    shareHolderList = FormatData("shareHolderList")
    legalPerson = ratingRequest.get("legalPerson")
    try:
        basicList = FormatData("basicList")
        regCap = basicList[0].get("regCap")
        enterprise_type = basicList[0].get("enterpriseType")
    except:
        return u'缺失值'

    # 20190514新增逻辑，当为个人独资企业返回1
    if u"个人独资企业" == enterprise_type:
        return 1
    if shareHolderList == [{}]:
        return u'缺失值'
    else:
        if len(shareHolderList) >= 1:
            resultList = ''
            sumSubConam = 0
            currSubConam = 0
            resultPercent = 0
            nameTotal = 0
            flag = True
            for i in shareHolderList:
                shareholderName = i.get("shareholderName")
                fundedRatio = i.get("fundedRatio")
                subConam = i.get("subConam")
                if shareholderName not in null_type_list and u"公司" in shareholderName and len(shareholderName) > 5:
                    nameTotal += 1
                if float_check(subConam):
                    sumSubConam += float(subConam)
                if shareholderName not in null_type_list and shareholderName == legalPerson:
                    flag = False
                    currSubConam = subConam
                    if resultList in null_type_list and fundedRatio not in null_type_list:
                        resultList = fundedRatio
            # 20190426新增逻辑，所有股东名称全包含公司则返回1
            if len(shareHolderList) == nameTotal:
                return 1

            if flag:
                return 0
            isPercent = "%" in resultList
            if isPercent:
                percent = resultList.replace("%", "")
                resultPercent = float(percent)/100.0 if float_check(percent) else u'缺失值'
            else:
                if float_check(resultList):
                    resultPercent = float(resultList) if float(resultList) < 1 else float(resultList)/100.0
            if resultPercent > 0:
                return '%.4f' % resultPercent
            else:
                if float_check(regCap) and float(regCap) == sumSubConam and float_check(currSubConam) and currSubConam not in null_type_list:
                    return '%.4f' % (float(currSubConam)/float(sumSubConam))
                return -999
        else:
            return u'缺失值'


if __name__ == "__main__":
    ratingRequest = {"legalPerson": u"朱为广", "legalPersonIdCard": "510102196706108511"}
    file_obj = open(r'../data/rating/R11_EG_CorporationShareRatioCheck/EG_DSJ.json', 'r')
    content = file_obj.read()
    EG_DSJ = json.loads(content, strict=False)
    aa = getRule11()
    print aa

result = getRule11()

